package ru.arriah.common.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.bean.GenericBean;
import ru.arriah.common.bean.MessageBean;
import ru.arriah.common.bean.PagingLinkBean;

public class DumbAction implements WebAction {
   protected Properties config;
   
   public DumbAction() {
      super();
   }
   
   public void setConfig(Properties config) {
     this.config = config;
   }

   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      return actionMapping.getSuccessTarget();
   }

//   /**
//    * @deprecated
//    * 
//    * @param genericBeanList
//    * @param id
//    */    
//   protected void setSelected(Collection genericBeanList, int id) {
//     Iterator iterator = genericBeanList.iterator();
//     GenericBean genericBean;
//     while(iterator.hasNext()) {
//       genericBean = (GenericBean)iterator.next();
//       if (genericBean.getId() == id) {
//         genericBean.setSelected();
//       }
//     }
//   }
      
   protected int idVal(String strVal) {
     try {
       return Integer.parseInt(strVal);     
     } catch (Exception e) {
       return 0;
     }
   }
   
   protected void addInfMessage(HttpServletRequest request, String message) {
      Collection<MessageBean> messagesList = 
         (Collection<MessageBean>)request.getAttribute("messagesList");
      
      messagesList.add(
         new MessageBean(message)
       );
   }
   
   protected void addErrorMessage(HttpServletRequest request, String message) {
      Collection<MessageBean> messagesList = 
         (Collection<MessageBean>)request.getAttribute("messagesList");
      
      messagesList.add(
         new MessageBean(message, true)
       );
   }
   
   /**
    * Get user id from session
    * @param request
    * @return
    * @deprecated Use {@link #getUserId(HttpServletRequest)} instead
    */
   protected int getCurrentUserId(HttpServletRequest request) {
      return getUserId(request);
   }

   /**
    * Get user id from session
    * @param request
    * @return
    */
   protected int getUserId(HttpServletRequest request) {
      return (Integer)request.getSession().getAttribute("userId");
   }
   
   
   protected void pageNavigationToRequest(
     int currentPage, 
     Integer totalItems,
     String url, 
     HttpServletRequest request
   ) {
    ArrayList<PagingLinkBean> pagingLinks = 
       new ArrayList<PagingLinkBean>();

    int itemsPerPage = Integer.parseInt(config.getProperty("ITEMS_PER_PAGE"));

    int sstart = 0;
    int send = 0;
    int add = 0;

    if (totalItems < itemsPerPage)
      return;

    if (currentPage != 0) {
      pagingLinks.add(new PagingLinkBean(url + "0", "&lt;&lt"));
      pagingLinks.add(new PagingLinkBean(url + (currentPage - 1), "&lt;"));
    } else {
      pagingLinks.add(new PagingLinkBean("", "&lt;&lt"));
      pagingLinks.add(new PagingLinkBean("", "&lt;"));
    }

    sstart = currentPage - 5;
    send = currentPage + 5;

    if (totalItems % itemsPerPage == 0)
      add = 0;
    else
      add = 1;

    if (send * itemsPerPage > totalItems)
      send = Math.round(totalItems / itemsPerPage) + add;
    if (sstart < 0)
      sstart = 0;

    for (int i = sstart; i < send; i++) {
      if (i == currentPage)
        pagingLinks.add(new PagingLinkBean("", (i + 1) + "/"
            + (Math.round(totalItems / itemsPerPage) + add)));
      else
        pagingLinks.add(new PagingLinkBean(url + i, (i + 1) + ""));
    }

    if (currentPage + (1 - add) < Math.round(totalItems / itemsPerPage)) {
      pagingLinks.add(new PagingLinkBean(url + (currentPage + 1), "&gt;"));
      pagingLinks.add(new PagingLinkBean(url
          + (Math.round(totalItems / itemsPerPage) - (1 - add)), "&gt;&gt;"));
    } else {
      pagingLinks.add(new PagingLinkBean("", "&gt;"));
      pagingLinks.add(new PagingLinkBean("", "&gt;&gt;"));
    }

    request.setAttribute("pagingLinks", pagingLinks);
    request.setAttribute("currentPage", currentPage);
  }
   
   
  /**
   * @author Mixa
   * @param registrationDate
   * @param startDate
   * @return
   */
   protected boolean sameDay(Date registrationDate, Date startDate) {
     Calendar registration = new GregorianCalendar();
     Calendar start = new GregorianCalendar();
     
     registration.setTime(registrationDate);
     start.setTime(startDate);
     
     return registration.get(Calendar.YEAR) == start.get(Calendar.YEAR) &&
            registration.get(Calendar.DAY_OF_YEAR) == start.get(Calendar.DAY_OF_YEAR) ? 
            true : false;
  }
   
   /**
    * @author Dr.John
    * @param e
    * @param methodName
    */
   protected void handleException(Exception e, String methodName) {
      System.out.println(
         this.getClass().getName() + "." + methodName + " caused " +
         e.getClass().getName() + ": " + e.getMessage()
      );

      e.printStackTrace();
   }
}